package com.api.browser.service.impl;

import com.alibaba.fastjson.JSON;
import com.api.browser.bean.SearchConditionItem;
import com.api.browser.bean.SplitTableBean;
import com.api.browser.bean.SplitTableColBean;
import com.api.browser.service.BrowserService;
import com.api.browser.util.*;
import com.cloudstore.dev.api.bean.SplitMobileDataBean;
import com.cloudstore.dev.api.bean.SplitMobileTemplateBean;
import org.apache.log4j.Logger;
import weaver.general.Util;
import weaver.systeminfo.SystemEnv;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class KsHrmBrow extends BrowserService {
    @Override
    public Map<String, Object> getBrowserData(Map<String, Object> map) throws Exception {
        Logger log = Logger.getLogger(Logger.class);
        List<SplitTableColBean> cols = new ArrayList<SplitTableColBean>();
        String depid= Util.null2String(map.get("zcsubid"));
        String lastname= Util.null2String(map.get("lastname"));
        String workcode= Util.null2String(map.get("workcode"));

        String backfields, fromSql, sqlwhere, orderby;


        backfields="id,workcode,lastname,lastnamefull";//返回字段
        fromSql=" from (select a.id,a.departmentid,a.workcode,a.lastname+'('+b.departmentname+'/'+c.subcompanyname+')' lastnamefull,a.lastname from hrmresource a left join hrmdepartment b on a.departmentid=b.id left join HrmSubCompany c on a.subcompanyid1=c.id where a.status in (0,1,2,3)  ) t ";//来源表
        sqlwhere=" 1=1   ";//where 条件
        if(!"".equals(depid)){
            if(depid.endsWith(",")){
                depid=depid.substring(0,depid.length()-1);
            }
            sqlwhere=sqlwhere+" and departmentid in ("+depid+")";
        }else{
            sqlwhere=" 1!=1 ";
        }
        if(!"".equals(lastname)){
            sqlwhere=sqlwhere+" and lastname like '%"+lastname+"%'";
        }
        if(!"".equals(workcode)){
            sqlwhere=sqlwhere+" and workcode like '%"+workcode+"%'";
        }
        orderby="workcode";//排序字段
        Map<String, Object> apidatas=new HashMap<String, Object>();

        cols.add(new SplitTableColBean("hide", "id"));
        cols.add(new SplitTableColBean("50%", SystemEnv.getHtmlLabelName(1268, user.getLanguage()), "lastnamefull", "lastnamefull", 1).setIsInputCol(BoolAttr.TRUE));
        cols.add(new SplitTableColBean("20%", SystemEnv.getHtmlLabelName(63, user.getLanguage()), "workcode", "workcode", 2));
        //cols.add(new SplitTableColBean("30%", SystemEnv.getHtmlLabelName(1278, user.getLanguage()), "manager", "manager",  3));
        SplitTableBean tableBean = new SplitTableBean(backfields, fromSql, sqlwhere, orderby, "id", cols);
        apidatas.putAll(SplitTableUtil.makeListDataResult(tableBean));
        tableBean.setMobileshowtype(MobileShowTypeAttr.ListView);
        try
        {
            SplitMobileTemplateBean localSplitMobileTemplateBean = new SplitMobileTemplateBean();
            localSplitMobileTemplateBean.addJsonTemplate("json", JSON.parseArray("[    {        \"configs\": [            {                \"configs\": [                    {                        \"key\": \"lastname\"                    }                ],                \"key\": \"col1_row1\"            },            {                \"configs\": [                    {                        \"key\": \"workcode\"                    },                ],                \"key\": \"col1_row2\"            }        ],        \"key\": \"col1\"    }]", SplitMobileDataBean.class));
            tableBean.createMobileTemplate(localSplitMobileTemplateBean);
        }
        catch (Exception localException)
        {
            log.error("localException--->"+localException.getMessage());
            localException.printStackTrace();
        }
        log.error("apidatasMyCustomBrow--->"+apidatas.toString());
        return apidatas;
    }
    @Override
    public Map<String, Object> getBrowserConditionInfo(Map<String, Object> arg0)
            throws Exception {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        Logger log = Logger.getLogger(Logger.class);
        try{

            // TODO Auto-generated method stub

            List<SearchConditionItem> conditions = new ArrayList<SearchConditionItem>();
            //User user = (User) arg0.get("user");
            ConditionFactory conditionFactory = new ConditionFactory(this.user);
            SearchConditionItem lastname= conditionFactory.createCondition(ConditionType.INPUT, 1268, "lastname");
            lastname.setLabel("姓名");
            conditions.add(lastname);
            SearchConditionItem workcode= conditionFactory.createCondition(ConditionType.INPUT, 17080, "workcode");
            workcode.setLabel("工号");
            conditions.add(workcode);
            //conditions.add(conditionFactory.createCondition(ConditionType.BROWSER, 63, "type"));
            //conditions.add(conditionFactory.createCondition(ConditionType.BROWSER, 602, "customerStatus", "cusstatus", 15078));

            //List<SearchConditionOption> statuOptions = new ArrayList<SearchConditionOption>();
            //statuOptions.add(new SearchConditionOption("",""));
            //statuOptions.add(new SearchConditionOption("1",SystemEnv.getHtmlLabelName(23773, user.getLanguage())));
            //statuOptions.add(new SearchConditionOption("2",SystemEnv.getHtmlLabelName(23774, user.getLanguage())));
            //conditions.add(conditionFactory.createCondition(ConditionType.SELECT, 19061, "status", statuOptions));

            apidatas.put("conditions", conditions);
            log.error("apidatas--->"+apidatas.toString());
        }catch(Exception e){
            log.error("getBrowserConditionInfo--->"+apidatas.toString());
        }
        return apidatas;

    }
}
